/**
 * Created by hooshim on 2014-12-03.
 */

var G_rowChecks = ""; // Grid 체크된 row들의 변수

//## Grid 스크롤 사이즈 구하기 ##################
var fnGetScrollSizes = function() {
    var el= document.createElement('div');
    el.style.display= 'hidden';
    el.style.overflow= 'scroll';
    document.body.appendChild(el);
    var h= el.offsetHeight-el.clientHeight;
    document.body.removeChild(el);
    return h;
}

//## Grid 헤더와 고정 Data 스크롤링 #################
var fnScrolling = function(grdCd) {
    $("#"+grdCd+"Header").scrollLeft($("#"+grdCd+"Body").scrollLeft());
    $("#"+grdCd+"BodyFix").scrollTop($("#"+grdCd+"Body").scrollTop());
}

//## Grid 초기화. ##################
var fnMkGridInit = function(grdInfo) {

    var gridInfo = $.parseJSON(grdInfo);
    //var gridLeftTopBgcolor = "#E5F1FD";
    //var gridCheckBgcolor = "#DDEEFF";
    var gridLeftTopBgcolor = "#F2F2F2";
    var gridCheckBgcolor = "#F2F2F2";

    var grdCd = gridInfo.grdCd;
    var gridHeight = gridInfo.fullHeight;
    var gridWidth = gridInfo.fullWidth;
    var gridFix = gridInfo.fix;
    var gridTitle = gridInfo.title;
    var scrollBarWidth = fnGetScrollSizes(); //get scroll bar's width.

    var gridBodyPos = $("#"+grdCd+"Body").offset();
    var gridLeftTop = 35;

    $("#"+grdCd+"Top").css("width", (parseInt(gridWidth,10) + 5) + "px");

    $("#"+grdCd+"Title").html(gridTitle);
    $("#"+grdCd+"Cnt").html('Total: 0');

    var leftPosition = gridBodyPos.left;
    if ($("#"+grdCd).css("float") == "right") leftPosition = parseInt(leftPosition,10) - parseInt(gridWidth,10) + 15;

    $("#"+grdCd+"Header").css("width", (parseInt(gridWidth,10) - parseInt(scrollBarWidth,10) + 5) + "px");
    $("#"+grdCd+"Header").css("height", "27px");
    $("#"+grdCd+"Header").css("top", (parseInt(gridBodyPos.top,10)-20) +"px");
    $("#"+grdCd+"Header").css("left", leftPosition+"px");
    $("#"+grdCd+"Header").css("position", "absolute");

    $("#"+grdCd+"Body").css("width", (parseInt(gridWidth,10) + 5) + "px");
    $("#"+grdCd+"Body").css("height", (gridHeight) + "px");
    $("#"+grdCd).css("width", (parseInt(gridWidth,10) + 5) + "px");

    var fullWidth = 0;
    var fullHeaders = '';
    for (c=0; c<gridInfo.widths.length; c++)
    {
        fullWidth += parseInt(gridInfo.widths[c],10)+1;
        fullHeaders += '<div style="width: '+gridInfo.widths[c]+'px" class="gridHeader"><p>'+gridInfo.titles[c]+'</p></div>';
    }

    fullWidth += parseInt(gridLeftTop,10) + 1;
    fullHeaders = '<div style="width: '+gridLeftTop+'px" class="gridHeader"><p>No</p></div>' + fullHeaders;

    //if ($("#"+grdCd).css("float") == "right") fullWidth = parseInt(fullWidth,10);
    var header = '<div style="min-width: '+fullWidth+'px;">' + fullHeaders + '</div>';
    $("#"+grdCd+"Header").append(header);

    //## Fixed Header
    var fixWidth = 0;
    var fixHeaders = '';

    for (d=0; d<gridFix; d++) {
        fixWidth += parseInt(gridInfo.widths[d], 10)+1;
        fixHeaders += '<div style="width: '+gridInfo.widths[d]+'px" class="gridHeaderFix"><p>'+gridInfo.titles[d]+'</p></div>';
    }
    fixWidth += parseInt(gridLeftTop,10) + 1;
    fixHeaders = '<div id="'+grdCd+'HeaderFix" style="width: '+gridLeftTop+'px" class="gridHeaderFixLeft"><p>&nbsp;</p></div>' + fixHeaders;
    var fixHeader =  '<div style="min-width: '+fixWidth+'px;position:absolute;top:'+ (parseInt(gridBodyPos.top,10)-20) +'px;left:'+leftPosition+'px;">';
        fixHeader += fixHeaders + '</div>';

    $('body').append(fixHeader);

    //$("#"+grdCd+"Body").append(header);

    var rows = '<div id="row00" style="min-width: '+fullWidth+'px;">';
    rows += '   <div style="width:'+(parseInt(fullWidth,10)-1)+'px;height:25px;" class="gridBody"><p style="padding-left:100px">No Result</p></div>';
    rows += '</div>';
    $("#"+grdCd+"Body").append(rows);

}

var fnGridResize = function(grdInfo, w) {
    var gridInfo = $.parseJSON(grdInfo);
    var curGridWidth = gridInfo.fullWidth;
    var grdCd = gridInfo.grdCd;
    var gridSize = parseInt(curGridWidth,10) + parseInt(w,10) - 17;
    $("#"+grdCd).css("width",gridSize+"px");
    $("#"+grdCd+"Header").css("width",gridSize+"px");
    $("#"+grdCd+"Body").css("width",gridSize+"px");
    $("#"+grdCd+"Top").css("width",gridSize+"px");
    var cntSize = gridSize-(gridSize / 2)-20;
    $("#"+grdCd+"Title").css("width",(gridSize / 2));
    $("#"+grdCd+"Cnt").css("width",cntSize);

    gridInfo.width = gridSize;

    var gInfo = new Object();
    gInfo["grdCd"] = gridInfo.grdCd;
    gInfo["title"] = gridInfo.title;
    gInfo["widths"] = gridInfo.widths;
    gInfo["titles"] = gridInfo.titles;
    gInfo["styles"] = gridInfo.styles;
    gInfo["fullWidth"] = gridSize;
    gInfo["fullHeight"] = gridInfo.fullHeight;
    gInfo["fix"] = gridInfo.fix;

    G_grid01 = JSON.stringify(gInfo);

}

//## Grid Data Appending ##################
var fnMkGrid = function(grdInfo, grdData) {

    //## 체크된 row 초기화
    G_rowChecks = "";

    var gridInfo = $.parseJSON(grdInfo);
    var gridLeftTopBgcolor = "#F2F2F2";
    var gridCheckBgcolor = "#F2F2F2";

    var grdCd = gridInfo.grdCd;
    var gridHeight = gridInfo.fullHeight;
    var gridWidth = gridInfo.fullWidth;
    var gridFix = gridInfo.fix;
    var gridTitle = gridInfo.title;
    var scrollBarWidth = fnGetScrollSizes(); //get scroll bar's width.

    var gridBodyPos = $("#"+grdCd+"Body").offset();
    var gridLeftTop = 35;

    $("#"+grdCd+"Top").css("width", (parseInt(gridWidth,10) + 5) + "px");

    $("#"+grdCd+"Title").html(gridTitle);
    $("#"+grdCd+"Cnt").html('Total: '+grdData.TOTAL_COUNT);

    var leftPosition = gridBodyPos.left;
    //if ($("#"+grdCd).css("float") == "right") leftPosition = parseInt(leftPosition,10) - parseInt(gridWidth,10) + 15;

    $("#"+grdCd+"Body").html("");
    $("#"+grdCd+"BodyFix").html("");
    $("#"+grdCd+"Body").css("width", (parseInt(gridWidth,10) + 5) + "px");
    $("#"+grdCd+"Body").css("height", (gridHeight) + "px");

    //'<div id="'+grdCd+'BodyFix" style="overflow: hidden;  height: '+(parseInt(gridHeight,10)-41)+'px; min-width: '+fixWidth+'px;position: absolute;top:'+(parseInt(gridBodyPos.top,10)+23)+'px;left:'+gridBodyPos.left+'px;">';

    $("#"+grdCd+"BodyFix").css("min-width", fixWidth + "px");
    $("#"+grdCd+"BodyFix").css("height", (parseInt(gridHeight,10)-41) + "px");
    $("#"+grdCd+"BodyFix").css("top", (parseInt(gridBodyPos.top,10)+23) + "px");
    $("#"+grdCd+"BodyFix").css("left", leftPosition + "px");
    $("#"+grdCd).css("width", (parseInt(gridWidth,10) + 5) + "px");

    var fullWidth = 0;
    var fullHeaders = '';
    for (c=0; c<gridInfo.widths.length; c++)
    {
        fullWidth += parseInt(gridInfo.widths[c],10)+1;
        fullHeaders += '<div style="width: '+gridInfo.widths[c]+'px" class="gridHeader"><p>'+gridInfo.titles[c]+'</p></div>';
    }

    fullWidth += parseInt(gridLeftTop,10) + 1;
    fullHeaders = '<div style="width: '+gridLeftTop+'px" class="gridHeader"><p>No</p></div>' + fullHeaders;

    var header = '<div style="min-width: '+fullWidth+'px;">' + fullHeaders + '</div>';

    //## Fixed Header
    var fixWidth = 0;
    var fixHeaders = '';

    for (d=0; d<gridFix; d++) {
        fixWidth += parseInt(gridInfo.widths[d], 10)+1;
        fixHeaders += '<div style="width: '+gridInfo.widths[d]+'px" class="gridHeaderFix"><p>'+gridInfo.titles[d]+'</p></div>';
    }
    fixWidth += parseInt(gridLeftTop,10) + 1;
    fixHeaders = '<div style="width: '+gridLeftTop+'px" class="gridHeaderFixLeft"><p>No</p></div>' + fixHeaders;
    var fixHeader =  '<div style="min-width: '+fixWidth+'px;position: absolute;top:'+ (parseInt(gridBodyPos.top,10)-20) +'px;left:'+leftPosition+'px;">';
    fixHeader += fixHeaders + '</div>';

    $("#"+grdCd+"Body").append(header);

    $("#"+grdCd+"BodyFix").css("display", "");

    // 데이터 그려내기
    var gridDataKey = grdData.KEYS.split(",");
    if (grdData.TOTAL_COUNT == 0)
    {
        var rows = '<div id="row00" style="min-width: '+fullWidth+'px;">';
        rows += '   <div style="width:'+(parseInt(fullWidth,10)-1)+'px;height:25px;" class="gridBody"><p style="padding-left:100px">No Result</p></div>';
        rows += '</div>';
        $("#"+grdCd+"Body").append(rows);
    }
    for(var r = 0; r < grdData.TOTAL_COUNT; r ++)
    {
        if(r%2 == 0) var bgColor = "background-color: #FFF;";
        else var bgColor = "background-color: "+gridCheckBgcolor+";";

        var rowData = grdData.GRIDDATA[r];
        var rows = '<div id="'+grdCd+'row'+r+'" style="min-width: '+fullWidth+'px;" onmouseover="fnSetLight(\''+grdCd+'\','+r+',\'on\')" onmouseout="fnSetLight(\''+grdCd+'\','+r+',\'off\')">';
        rows += '<div style="width:'+gridLeftTop+'px;background-color:'+gridLeftTopBgcolor+';" class="gridBody"><p>'+(parseInt(r,10)+1)+'</p></div>'
        var func = "";
        var linkCd = "";
        for (k=0; k<gridDataKey.length; k++)
        {
            var cellData = rowData[gridDataKey[k]];
            if(!cellData || cellData == ' ') cellData = "&nbsp;";
            if (k == 0) {

                if (gridInfo.function != undefined)
                {
                    linkCd = rowData[gridDataKey[gridDataKey.length-1]];

                    func = "onclick=\""+gridInfo.function+"('"+linkCd+"')\"";
                }
                if (func) bgColor += "cursor:pointer;";
            }
            if (gridDataKey[k] != "LINK") {
                rows += '<div style="width:'+gridInfo.widths[k]+'px;'+gridInfo.styles[k]+bgColor+'" '+func+'class="gridBody"><p>'+cellData+'</p></div>';
            }

        }
        rows += '</div>';
        //alert(rows);
        $("#"+grdCd+"Body").append(rows);

        // 프리징 Data 그리기
        if(r%2 == 0) var bgColor = "background-color: #FFF;";
        else var bgColor = "background-color: "+gridCheckBgcolor+";";
        var fixBody = '<div id="'+grdCd+'fix'+r+'" cd="'+linkCd+'" style="min-width: '+fixWidth+'px;left:'+leftPosition+'px;" onmouseover="fnSetLight(\''+grdCd+'\','+r+',\'on\')" onmouseout="fnSetLight(\''+grdCd+'\','+r+',\'off\')">';
        fixBody += '<div id="'+grdCd+'-'+r+'" style="width:'+gridLeftTop+'px;background-color:'+gridLeftTopBgcolor+';cursor:pointer;" onClick="fnChkRowCheck(\''+grdCd+'\','+r+')" class="gridBodyFixLeft"><p>'+(parseInt(r,10)+1)+'</p></div>'
        for (d=0; d<gridFix; d++) {
            var cellData = rowData[gridDataKey[d]];
            if(!cellData || cellData == ' ') cellData = "&nbsp;";
            fixBody += '<div style="width:'+gridInfo.widths[d]+'px;'+gridInfo.styles[d]+bgColor+bgColor+'" '+func+'class="gridBodyFix"><p>'+cellData+'</p></div>';
        }
        fixBody += '</div>';
        $("#"+grdCd+"BodyFix").append(fixBody);
    }

    $("#"+grdCd+"Header").scrollLeft(0);
    $("#"+grdCd+"HeaderFix").scrollLeft(0);
    $("#"+grdCd+"Body").scrollLeft(0);
    $("#"+grdCd+"BodyFix").scrollTop(0);
}

//## Grid 로딩..... ###################
var fnMkGridLoad = function(grdInfo) {

    var gridInfo = $.parseJSON(grdInfo);
    var gridLeftTopBgcolor = "#F2F2F2";
    var gridCheckBgcolor = "#F2F2F2";

    var grdCd = gridInfo.grdCd;
    var gridHeight = gridInfo.fullHeight;
    var gridWidth = gridInfo.fullWidth;
    var gridFix = gridInfo.fix;
    var gridTitle = gridInfo.title;
    var scrollBarWidth = fnGetScrollSizes(); //get scroll bar's width.

    var gridBodyPos = $("#"+grdCd+"Body").offset();
    var gridLeftTop = 35;

    $("#"+grdCd+"Top").css("width", (parseInt(gridWidth,10) + 5) + "px");

    $("#"+grdCd+"Title").html(gridTitle);
    $("#"+grdCd+"Cnt").html('Total: Searching...');

    var leftPosition = gridBodyPos.left;
    if ($("#"+grdCd).css("float") == "right") leftPosition = parseInt(leftPosition,10) - parseInt(gridWidth,10) + 15;

    $("#"+grdCd+"Body").html("");
    $("#"+grdCd+"BodyFix").html("");
    $("#"+grdCd+"Body").css("width", (parseInt(gridWidth,10) + 5) + "px");
    $("#"+grdCd+"Body").css("height", (gridHeight) + "px");

    //'<div id="'+grdCd+'BodyFix" style="overflow: hidden;  height: '+(parseInt(gridHeight,10)-41)+'px; min-width: '+fixWidth+'px;position: absolute;top:'+(parseInt(gridBodyPos.top,10)+23)+'px;left:'+gridBodyPos.left+'px;">';

    $("#"+grdCd+"BodyFix").css("min-width", fixWidth + "px");
    $("#"+grdCd+"BodyFix").css("height", (parseInt(gridHeight,10)-41) + "px");
    $("#"+grdCd+"BodyFix").css("top", (parseInt(gridBodyPos.top,10)+23) + "px");
    $("#"+grdCd+"BodyFix").css("left", leftPosition + "px");
    $("#"+grdCd).css("width", (parseInt(gridWidth,10) + 5) + "px");


    var fullWidth = 0;
    var fullHeaders = '';
    for (c=0; c<gridInfo.widths.length; c++)
    {
        fullWidth += parseInt(gridInfo.widths[c],10)+1;
        fullHeaders += '<div style="width: '+gridInfo.widths[c]+'px" class="gridHeader"><p>'+gridInfo.titles[c]+'</p></div>';
    }

    fullWidth += parseInt(gridLeftTop,10) + 1;
    fullHeaders = '<div style="width: '+gridLeftTop+'px" class="gridHeader"><p>No</p></div>' + fullHeaders;

    var header = '<div style="min-width: '+fullWidth+'px;">' + fullHeaders + '</div>';

    //## Fixed Header
    var fixWidth = 0;
    var fixHeaders = '';

    for (d=0; d<gridFix; d++) {
        fixWidth += parseInt(gridInfo.widths[d], 10)+1;
        fixHeaders += '<div style="width: '+gridInfo.widths[d]+'px" class="gridHeaderFix"><p>'+gridInfo.titles[d]+'</p></div>';
    }
    fixWidth += parseInt(gridLeftTop,10) + 1;
    fixHeaders = '<div style="width: '+gridLeftTop+'px" class="gridHeaderFix"><p>No</p></div>' + fixHeaders;
    var fixHeader =  '<div style="min-width: '+fixWidth+'px;position: absolute;top:'+ (parseInt(gridBodyPos.top,10)-20) +'px;left:'+leftPosition+'px;">';
    fixHeader += fixHeaders + '</div>';

    $("#"+grdCd+"Body").append(header);

    fullWidth += parseInt(gridLeftTop,10) + 1;
    var rows = '<div id="row00" style="min-width: '+fullWidth+'px;">';
    rows += '   <div style="width:'+(parseInt(fullWidth,10)-1)+'px;height:25px;" class="gridBody"><p>Searching....</p></div>';
    rows += '</div>';
    $("#"+grdCd+"Body").append(rows);

}

//## Grid row 체크여부 등록 ##################
var fnChkRowCheck = function(grdCd, r) {
    if (G_rowChecks.indexOf("/"+grdCd+"-"+r+"/") < 0) G_rowChecks += "/"+grdCd+"-"+r+"/";
    else G_rowChecks = G_rowChecks.split("/"+grdCd+"-"+r+"/").join("");
}

//## Grid Row MouseOver.... ##################
var fnSetLight = function(grdCd, r,onOff) {
    if (onOff == "on") {
        if (G_rowChecks.indexOf("/"+grdCd+"-"+r+"/") >= 0) {
            $("#" + grdCd + "-" + r).css("background-image", "url(/img/rowCheck.jpg)");
            $("#" + grdCd + "-" + r).css("background-repeat", "no-repeat");
            $("#" + grdCd + "-" + r).css('background-position', 'center');
            $("#" + grdCd + "-" + r).css('font-weight', 'bold');
            //$("#row" + r).css('font-weight', 'bold');
            //$("#fix" + r).css('font-weight', 'bold');
        }
        $("#" + grdCd + "fix" + r).css("border-bottom", "1px #ff9900 solid");
        $("#" + grdCd + "fix" + r).css("border-top", "1px #ff9900 solid");
        $("#" + grdCd + "fix" + r).css("border-left", "1px #ff9900 solid");
        $("#" + grdCd + "row" + r).css("border-top", "1px #ff9900 solid");
        $("#" + grdCd + "row" + r).css("border-bottom", "1px #ff9900 solid");
        $("#" + grdCd + "row" + r).css("border-right", "1px #ff9900 solid");
    }
    else
    {
        if (G_rowChecks.indexOf("/"+grdCd+"-"+r+"/") < 0) {
            $("#" + grdCd + "fix" + r).css("border", "0");
            $("#" + grdCd + "row" + r).css("border", "0");
            $("#" + grdCd + "-" + r).css("background-image", "");
            $("#" + grdCd + "-" + r).css('font-weight', '');
            //$("#row" + r).css('font-weight', '');
            //$("#fix" + r).css('font-weight', '');
        }
    }
}